#!/bin/bash

# TMS模块重构脚本: 将 productInfo 重命名为 order
# 执行前请确保代码已提交到git,以便回滚

set -e

echo "=========================================="
echo "TMS模块重构: productInfo -> order"
echo "=========================================="
echo ""

# 定义基础路径
BASE_PATH="/Users/glenn/github/ruoyi-vue-pro/yudao-module-tms"
cd "$BASE_PATH"

echo "步骤 1: 备份当前状态..."
# git stash save "Before productInfo to order refactoring"

echo ""
echo "步骤 2: 重命名目录结构..."

# 重命名 Java 包目录
echo "  - 重命名 controller/admin/productinfo -> order"
if [ -d "src/main/java/cn/iocoder/yudao/module/tms/controller/admin/productinfo" ]; then
    git mv src/main/java/cn/iocoder/yudao/module/tms/controller/admin/productinfo \
           src/main/java/cn/iocoder/yudao/module/tms/controller/admin/order
fi

echo "  - 重命名 dal/dataobject/productinfo -> order"
if [ -d "src/main/java/cn/iocoder/yudao/module/tms/dal/dataobject/productinfo" ]; then
    git mv src/main/java/cn/iocoder/yudao/module/tms/dal/dataobject/productinfo \
           src/main/java/cn/iocoder/yudao/module/tms/dal/dataobject/order
fi

echo "  - 重命名 dal/mysql/productinfo -> order"
if [ -d "src/main/java/cn/iocoder/yudao/module/tms/dal/mysql/productinfo" ]; then
    git mv src/main/java/cn/iocoder/yudao/module/tms/dal/mysql/productinfo \
           src/main/java/cn/iocoder/yudao/module/tms/dal/mysql/order
fi

echo "  - 重命名 service/productinfo -> order"
if [ -d "src/main/java/cn/iocoder/yudao/module/tms/service/productinfo" ]; then
    git mv src/main/java/cn/iocoder/yudao/module/tms/service/productinfo \
           src/main/java/cn/iocoder/yudao/module/tms/service/order
fi

echo "  - 重命名 mapper/productinfo -> order"
if [ -d "src/main/resources/mapper/productinfo" ]; then
    git mv src/main/resources/mapper/productinfo \
           src/main/resources/mapper/order
fi

echo ""
echo "步骤 3: 重命名文件..."

# 在新目录中重命名文件
cd "$BASE_PATH"

# Controller
if [ -f "src/main/java/cn/iocoder/yudao/module/tms/controller/admin/order/ProductInfoController.java" ]; then
    git mv src/main/java/cn/iocoder/yudao/module/tms/controller/admin/order/ProductInfoController.java \
           src/main/java/cn/iocoder/yudao/module/tms/controller/admin/order/OrderController.java
fi

# VO
if [ -f "src/main/java/cn/iocoder/yudao/module/tms/controller/admin/order/vo/ProductInfoPageReqVO.java" ]; then
    git mv src/main/java/cn/iocoder/yudao/module/tms/controller/admin/order/vo/ProductInfoPageReqVO.java \
           src/main/java/cn/iocoder/yudao/module/tms/controller/admin/order/vo/OrderPageReqVO.java
fi

if [ -f "src/main/java/cn/iocoder/yudao/module/tms/controller/admin/order/vo/ProductInfoRespVO.java" ]; then
    git mv src/main/java/cn/iocoder/yudao/module/tms/controller/admin/order/vo/ProductInfoRespVO.java \
           src/main/java/cn/iocoder/yudao/module/tms/controller/admin/order/vo/OrderRespVO.java
fi

if [ -f "src/main/java/cn/iocoder/yudao/module/tms/controller/admin/order/vo/ProductInfoSaveReqVO.java" ]; then
    git mv src/main/java/cn/iocoder/yudao/module/tms/controller/admin/order/vo/ProductInfoSaveReqVO.java \
           src/main/java/cn/iocoder/yudao/module/tms/controller/admin/order/vo/OrderSaveReqVO.java
fi

# DO
if [ -f "src/main/java/cn/iocoder/yudao/module/tms/dal/dataobject/order/ProductInfoDO.java" ]; then
    git mv src/main/java/cn/iocoder/yudao/module/tms/dal/dataobject/order/ProductInfoDO.java \
           src/main/java/cn/iocoder/yudao/module/tms/dal/dataobject/order/OrderDO.java
fi

if [ -f "src/main/java/cn/iocoder/yudao/module/tms/dal/dataobject/order/ProductInfoRefImageDO.java" ]; then
    git mv src/main/java/cn/iocoder/yudao/module/tms/dal/dataobject/order/ProductInfoRefImageDO.java \
           src/main/java/cn/iocoder/yudao/module/tms/dal/dataobject/order/OrderRefImageDO.java
fi

# Mapper
if [ -f "src/main/java/cn/iocoder/yudao/module/tms/dal/mysql/order/ProductInfoMapper.java" ]; then
    git mv src/main/java/cn/iocoder/yudao/module/tms/dal/mysql/order/ProductInfoMapper.java \
           src/main/java/cn/iocoder/yudao/module/tms/dal/mysql/order/OrderMapper.java
fi

if [ -f "src/main/java/cn/iocoder/yudao/module/tms/dal/mysql/order/ProductInfoRefImageMapper.java" ]; then
    git mv src/main/java/cn/iocoder/yudao/module/tms/dal/mysql/order/ProductInfoRefImageMapper.java \
           src/main/java/cn/iocoder/yudao/module/tms/dal/mysql/order/OrderRefImageMapper.java
fi

# Service
if [ -f "src/main/java/cn/iocoder/yudao/module/tms/service/order/ProductInfoService.java" ]; then
    git mv src/main/java/cn/iocoder/yudao/module/tms/service/order/ProductInfoService.java \
           src/main/java/cn/iocoder/yudao/module/tms/service/order/OrderService.java
fi

if [ -f "src/main/java/cn/iocoder/yudao/module/tms/service/order/ProductInfoServiceImpl.java" ]; then
    git mv src/main/java/cn/iocoder/yudao/module/tms/service/order/ProductInfoServiceImpl.java \
           src/main/java/cn/iocoder/yudao/module/tms/service/order/OrderServiceImpl.java
fi

if [ -f "src/main/java/cn/iocoder/yudao/module/tms/service/order/ProductInfoRefImageService.java" ]; then
    git mv src/main/java/cn/iocoder/yudao/module/tms/service/order/ProductInfoRefImageService.java \
           src/main/java/cn/iocoder/yudao/module/tms/service/order/OrderRefImageService.java
fi

if [ -f "src/main/java/cn/iocoder/yudao/module/tms/service/order/ProductInfoRefImageServiceImpl.java" ]; then
    git mv src/main/java/cn/iocoder/yudao/module/tms/service/order/ProductInfoRefImageServiceImpl.java \
           src/main/java/cn/iocoder/yudao/module/tms/service/order/OrderRefImageServiceImpl.java
fi

# Mapper XML
if [ -f "src/main/resources/mapper/order/ProductInfoMapper.xml" ]; then
    git mv src/main/resources/mapper/order/ProductInfoMapper.xml \
           src/main/resources/mapper/order/OrderMapper.xml
fi

if [ -f "src/main/resources/mapper/order/ProductInfoRefImageMapper.xml" ]; then
    git mv src/main/resources/mapper/order/ProductInfoRefImageMapper.xml \
           src/main/resources/mapper/order/OrderRefImageMapper.xml
fi

echo ""
echo "步骤 4: 替换文件内容中的类名和包名..."

# 使用 find 和 sed 批量替换
find src/main/java -type f -name "*.java" -exec sed -i '' \
    -e 's/productinfo/order/g' \
    -e 's/ProductInfo/Order/g' \
    -e 's/product_info/tms_order/g' \
    {} +

# 替换 XML 文件
find src/main/resources/mapper -type f -name "*.xml" -exec sed -i '' \
    -e 's/productinfo/order/g' \
    -e 's/ProductInfo/Order/g' \
    -e 's/product_info/tms_order/g' \
    {} +

# 替换统计相关的 XML (保留 tms_product_info 表名,因为这是实际的数据库表)
# 注意: 数据库表名 tms_product_info 需要单独处理,可能需要数据库迁移脚本

echo ""
echo "步骤 5: 清理编译文件..."
if [ -d "target" ]; then
    rm -rf target
fi

echo ""
echo "=========================================="
echo "重构完成!"
echo "=========================================="
echo ""
echo "下一步操作:"
echo "1. 检查代码变更: git status"
echo "2. 编译项目验证: mvn clean compile"
echo "3. 如果有问题,回滚: git reset --hard HEAD"
echo "4. 提交变更: git add . && git commit -m 'refactor: rename productInfo to order in TMS module'"
echo ""
